﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Reflection;
using System.Diagnostics;
using System.Configuration; 
using Wedo.Utility.Log;
using Wedo.Utility.ExportFile;
using Wedo.Recognition.Domain.Models; 
using Wedo.Recognition.Service.BankImport;
using Wedo.Recognition.Service; 

namespace Wedo.IPRP.RecieveVoucher.JOB
{
    class Program
    {
        static void Main(string[] args)
        {

             RecieveService bankDataBusiness = new RecieveService(Container_Config.GetIUnitWork());
            
            //创建收款凭证记录
            bankDataBusiness.CreateRecieveRecord();
            //获取收款凭证记录
            var group = bankDataBusiness.GetReceiveRecordsGroup();

            if (group != null && group.Count > 0)
            {
                string filepath = System.IO.Path.Combine(
                         ConfigurationManager.AppSettings["RecieveVoucherPath"],
                         DateTime.Now.ToString("yyyyMMddHHmmss")
                );

                foreach (var item in group)
                {
                    string filename = string.Format("RV{0}.csv", item.Key.ToString("yyyyMMdd"));

                    string path = System.IO.Path.Combine(filepath, filename);
                    //是否成功生成文件
                    if (OutPutRecieveFile(item, path))
                    {
                        GenerateRecieve model = new GenerateRecieve();
                        model.GenerateRecieveFileName = path;
                        model.AccountDate = item.Key;
                        model.Created = DateTime.Now;
                        model.GenerateRecieveDate = DateTime.Now;
                        model.Modified = DateTime.Now;
                        model.CreatedBy = "System";
                        model.ModifiedBy = "System";

                        bankDataBusiness.Add(model);

                        //收款凭证记录生成成功
                        bankDataBusiness.UpdateRecievStatusByIDs(item, 1);
                    }
                    else
                    {
                        //收款凭证记录生成失败
                        bankDataBusiness.UpdateRecievStatusByIDs(item, 2);
                    }
                }
            }
            Logger.LogDebug("Wedo.IPRP.RecieveVoucher.JOB", "", string.Format("{0} Success.", DateTime.Now), null);
        }

        /// <summary>
        /// 输出收账单文件
        /// </summary>
        public static bool OutPutRecieveFile(IGrouping<DateTime, RecieveRecord> recieveVoucher, string path)
        {
            if (recieveVoucher == null)
                return false;
            try
            {
                var csv = new BaseExportCsv<RecieveRecord>(path, recieveVoucher, "RecieveVoucher");
                csv.Export();
                return true;
            }
            catch (ExportFileFailure exportFileFailure)
            {
                Logger.LogError("", "OutPutRecieveFile", exportFileFailure, "", "");
            }
            catch (ExportFileExist exportFileExist)
            {
                Logger.LogError("", "OutPutRecieveFile", exportFileExist, "", "");
            }
            catch (Exception exception)
            {
                Logger.LogError("", "OutPutRecieveFile", exception, "", "");
            }
            return false;
        }
//#if DEBUG
//        /// <summary>
//        /// 输出收账单
//        /// </summary>
//        /// <param name="recieveVoucher"></param>
//        /// <param name="filePath"></param>
//        /// <param name="fileName"></param>
//        /// <returns></returns>
//        public static bool OutPutRecieveFile(dynamic recieveVoucher,string filePath,string fileName)
//        {
//            foreach (var _recieveVoucher in recieveVoucher)
//            {
//                Type typeRecieveVoucher = _recieveVoucher.GetType();

//                foreach (var _property in typeRecieveVoucher.GetProperties(BindingFlags.Public | BindingFlags.Instance))
//                {
//                    Console.Write("{0}:{1}\t", _property.Name, _property.GetValue(_recieveVoucher, _property.GetIndexParameters()));
//                }
//                Console.WriteLine("\r\n");
//            }
//            return false;
//        }
//#else


        ///// <summary>
        ///// 输出收账单文件
        ///// </summary>
        //public static List<GenerateRecieve> OutPutRecieveFile(Dictionary<DateTime, List<RecieveRecord>> recieveVoucher)
        //{
        //    List<GenerateRecieve> list = new List<GenerateRecieve>();
        //    if (recieveVoucher == null || recieveVoucher.Count == 0)
        //        return list;

        //    string filepath = System.IO.Path.Combine(
        //             ConfigurationManager.AppSettings["RecieveVoucherPath"],
        //             DateTime.Now.ToString("yyyyMMddHHmmss")
        //         );

        //    foreach (var item in recieveVoucher)
        //    {
        //        string filename = string.Format("RV{0}.csv", item.Key.ToString("yyyyMMdd"));

        //        string path = System.IO.Path.Combine(filepath, filename);
        //        DateTime now = DateTime.Now;
        //        try
        //        {
        //            var csv = new BaseExportCsv<RecieveRecord>(path, item.Value, "RecieveVoucher");
        //            csv.Export();
        //            list.Add(new GenerateRecieve
        //            {
        //                GenerateRecieveFileName = path,
        //                AccountDate = item.Key,
        //                Created = now,
        //                GenerateRecieveDate = now,
        //                Modified = now,
        //            });
        //        }
        //        catch (ExportFileFailure exportFileFailure)
        //        {
        //            Logger.LogError("", "OutPutRecieveFile", exportFileFailure, "", "");
        //        }
        //        catch (ExportFileExist exportFileExist)
        //        {
        //            Logger.LogError("", "OutPutRecieveFile", exportFileExist, "", "");
        //        }
        //        catch (Exception exception)
        //        {
        //            Logger.LogError("", "OutPutRecieveFile", exception, "", "");
        //        }
        //    }

        //    return list;
        //}
 //#endif
    }
}
